package com.cn.xiaonuo.slbfinances.slbreward.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cn.xiaonuo.common.constant.SystemConstant;
import com.cn.xiaonuo.common.entity.BusinessMessage;
import com.cn.xiaonuo.common.utils.StringUtils;
import com.cn.xiaonuo.dbs.core.annotation.DataSource;
import com.cn.xiaonuo.slbfinances.slbreward.entity.SlPrize;
import com.cn.xiaonuo.slbfinances.slbreward.entity.SlSystemLogs;
import com.cn.xiaonuo.slbfinances.slbreward.mapper.CmPrizeLogMapper;
import com.cn.xiaonuo.slbfinances.slbreward.mapper.SlPrizeMapper;
import com.cn.xiaonuo.slbfinances.slbreward.mapper.SlSystemLogMapper;
import com.cn.xiaonuo.slbfinances.slbreward.service.ICoveragePrizeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class CoveragePrizeServiceImpl extends ServiceImpl<CmPrizeLogMapper, String> implements ICoveragePrizeService {

    @Autowired
    private CmPrizeLogMapper cmPrizeLogMapper;
    @Autowired
    private SlPrizeMapper slPrizeMapper;
    @Autowired
    private SlSystemLogMapper slSystemLogsMapper;

    @Override
    @DataSource(name = "jyb_soole_data_hn")
    public BusinessMessage myPrizeLog(String userId, String phone, String beginTime, String endTime, String prizeGrade, String useState, Integer pageNum, Integer pageSize) {
        BusinessMessage message = new BusinessMessage<>();
        try {
            //1.如果时间不为空则转换时间格式
            if (StringUtils.isNotBlank(beginTime)) {
                beginTime = SystemConstant.SDF.format(new Date(new Long(beginTime)));
            }
            if (StringUtils.isNotBlank(endTime)) {
                endTime = SystemConstant.SDF.format(new Date(new Long(endTime)));
            }

            //2.设置默认分页参数
            if (null == pageNum || pageNum <= 1) {
                pageNum = 1;
            }
            if (null == pageSize || pageSize <= 1) {
                pageSize = 10;
            }

            //3.执行查询
            PageHelper.startPage(pageNum, pageSize);
            List<Map<String, Object>> maps = cmPrizeLogMapper.myPrizeLog(userId, phone, beginTime, endTime, prizeGrade, useState);
            PageInfo<Map<String, Object>> data = new PageInfo<>(maps);

            message.setData(data);
            message.setSuccess(true);
            message.setMessage("操作成功");
            return message;
        } catch (Exception e) {
            e.printStackTrace();
            message.setSuccess(false);
            message.setMessage("操作失败");
            return message;
        }
    }

    /*抽奖规则设置修改*/
    @Override
    @DataSource(name = "jyb_soole_data_hn")
    public BusinessMessage updatePrize(String loginUserId, String prizeId, String prizeGrade, String prizeName, String prizeWeight, String prizeAmount) {
        BusinessMessage message = new BusinessMessage<>();
        try {
            //1.验证参数
            if (StringUtils.isBlank(loginUserId) || StringUtils.isBlank(prizeId)) {
                message.setSuccess(false);
                message.setMessage("必填参数不能为空");
                return message;
            }
            //2.查询原来数据
            SlPrize slPrize = slPrizeMapper.selectById(prizeId);
            if (slPrize == null) {
                message.setSuccess(false);
                message.setMessage("该奖品不存在");
                return message;
            }
            if (StringUtils.isNotBlank(prizeGrade)) {
                slPrize.setPrizeGrade(prizeGrade);
            }
            if (StringUtils.isNotBlank(prizeName)) {
                slPrize.setPrizeName(prizeName);
            }
            if (StringUtils.isNotBlank(prizeWeight)) {
                slPrize.setPrizeWeight(Integer.parseInt(prizeWeight));
            }
            if (StringUtils.isNotBlank(prizeAmount)) {
                slPrize.setPrizeAmount(Double.parseDouble(prizeAmount));
            }
            slPrizeMapper.updateById(slPrize);

            //3.记录操作日志
            SlSystemLogs slSystemLogs = new SlSystemLogs();
            slSystemLogs.setUpdatedAt(new Date());
            slSystemLogs.setCreatedAt(new Date());
            slSystemLogs.setCreateTime((int) (System.currentTimeMillis() / 1000));
            slSystemLogs.setTitle("抽奖参数修改");
            slSystemLogs.setContent("抽奖参数修改");
            slSystemLogs.setAction("抽奖参数修改");
            slSystemLogs.setUsername("后台人员");
            slSystemLogs.setType(true);
            slSystemLogs.setUserid(Integer.parseInt(loginUserId));
            slSystemLogsMapper.insert(slSystemLogs);
            message.setSuccess(true);
            message.setMessage("操作成功");
            return message;
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            e.printStackTrace();
            message.setSuccess(false);
            message.setMessage("操作失败");
            return message;
        }
    }

    /*抽奖规则配置修改*/
    @Override
    @DataSource(name = "jyb_soole_data_hn")
    @Transactional
    public BusinessMessage updatePrizeConfig(String loginUserId, String configId, String configValue) {
        BusinessMessage message = new BusinessMessage<>();
        try {
            //1.解析参数
            if (StringUtils.isBlank(loginUserId) || StringUtils.isBlank(configId) || StringUtils.isBlank(configValue)) {
                message.setSuccess(false);
                message.setMessage("必填参数不能为空");
                return message;
            }
            //2.执行修改
            cmPrizeLogMapper.updatePrizeConfig(configValue, configId);
            //3.记录操作日志
            SlSystemLogs slSystemLogs = new SlSystemLogs();
            slSystemLogs.setUpdatedAt(new Date());
            slSystemLogs.setCreatedAt(new Date());
            slSystemLogs.setCreateTime((int) (System.currentTimeMillis() / 1000));
            slSystemLogs.setTitle("抽奖参数修改");
            slSystemLogs.setContent("抽奖参数修改");
            slSystemLogs.setAction("抽奖参数修改");
            slSystemLogs.setUsername("后台人员");
            slSystemLogs.setType(true);
            slSystemLogs.setUserid(Integer.parseInt(loginUserId));
            slSystemLogsMapper.insert(slSystemLogs);
            TransactionAspectSupport.currentTransactionStatus().flush();
            message.setSuccess(true);
            message.setMessage("操作成功");
            return message;
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            e.printStackTrace();
            message.setSuccess(false);
            message.setMessage("操作失败");
            return message;
        }
    }

    /*抽奖规则列表*/
    @Override
    @DataSource(name = "jyb_soole_data_hn")
    public BusinessMessage queryPrize() {
        BusinessMessage message = new BusinessMessage<>();
        try {
            QueryWrapper queryWrapper = new QueryWrapper();
            List<SlPrize> prizeList = slPrizeMapper.selectList(queryWrapper);
            message.setData(prizeList);
            message.setSuccess(true);
            message.setMessage("操作成功");
            return message;
        } catch (Exception e) {
            e.printStackTrace();
            message.setSuccess(false);
            message.setMessage("操作失败");
            return message;
        }
    }

    /*抽奖规则配置*/
    @Override
    @DataSource(name = "jyb_soole_data_hn")
    public BusinessMessage queryPrizeConfig() {
        BusinessMessage message = new BusinessMessage<>();
        try {
            List<Map<String, Object>> maps = cmPrizeLogMapper.queryPrizeConfig();
            message.setData(maps);
            message.setSuccess(true);
            message.setMessage("操作成功");
            return message;
        } catch (Exception e) {
            e.printStackTrace();
            message.setSuccess(false);
            message.setMessage("操作失败");
            return message;
        }
    }

}
